package cn.com.atlasdata.atops.controller;

import cn.com.atlasdata.atops.helper.Config;
import cn.com.atlasdata.atops.helper.Util;
import cn.com.atlasdata.atops.module.Api;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @description api接口类
 * @author ZJH
 * @create 2018-08-27 15:42
 **/
@RestController
@RequestMapping(value = "/api")
@EnableConfigurationProperties({Config.class})
public class ApiController {
    @Autowired
    private Config config;

    /*
      测试连接
   */
    @ResponseBody
    @RequestMapping(value = "/test", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String test(HttpServletRequest request){
        return "success";
    }

    /*
        获取数据库类型及数量
     */
    @ResponseBody
    @RequestMapping(value = "/database_info", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String getDatabaseInfo(HttpServletRequest request){
        JSONObject jsonParam = Util.getJsonParam(request);
        String type = jsonParam.getString("type");
        return Api.getDatabaseInfo(type,config);
    }

    /*
     获取top N
  */
    @ResponseBody
    @RequestMapping(value = "/top_n")
    public String getTopN(HttpServletRequest request){
        JSONObject jsonParam = Util.getJsonParam(request);
        String type = jsonParam.getString("type");
        String net_type = "";
        if ("net".equals(type)) {
             net_type = jsonParam.getString("net_type");
             if (null == net_type ||"".equals(net_type)){//默认net_type为net_usage
                 net_type = "net_usage";
             }
        }

        return Api.getTopN(type,net_type,config);
    }

    /*
     获取延迟机器信息
  */
    @ResponseBody
    @RequestMapping(value = "/delayed_db")
    public String getDelayedDb(){
        return  Api.getDelayedDb(config);
    }

    /*
      获取top SQL
   */
    @ResponseBody
    @RequestMapping(value = "/top_sql", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String getTopSql(HttpServletRequest request){
        JSONObject jsonParam = Util.getJsonParam(request);
        String endpoint = jsonParam.getString("endpoint");
        String orderBy = jsonParam.getString("oder_by");
        if (null == orderBy ||"".equals(orderBy)){
            orderBy = "executions";
        }
        int sum = 10;
        if(jsonParam.containsKey("sum")){
            sum = jsonParam.getInteger("sum");
        }
        return  Api.getTopSql(endpoint,orderBy,sum, config);
    }


    //*********************************************保存接口****************************************//
    /*
    保存延迟机器接口
 */
    @ResponseBody
    @RequestMapping(value = "/push/delayed_db", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String saveDelayedDb(HttpServletRequest request){

        return  Api.saveDelayedDb(Util.getJsonParam(request),config);
    }


    /*
    保存top SQL接口
 */
    @ResponseBody
    @RequestMapping(value = "/push/top_sql", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String saveTopSql(HttpServletRequest request){
        return  Api.saveTopSql(Util.getJsonParam(request),config);
    }
}
